﻿<cfscript>
/** 
* @hint "chooseManager - StudentCourse, ProgramTask, CommonTask"
*/ 

component 
	extends="MachII.framework.Listener" 
	displayname="acadmicManager.listeners.senate.studentCourseListener" 
	output=false 
	depends="datetimeAdvice, choosePropertyAdvice, sequenceStudentCourse, sequenceStudentCourse, sequenceProgramTask, logFactory" 
	{
	
	/**
	* @hint "Configures this listener as part of the Mach-II framework"
	*/
	public void function configure() output=false {} 
	
	
	/**
	* @hint "更改学生选课记录课程修读方式"
	* 
	* @event "MachII Event Object"
	*/
	public void function modifyStudentCourseStudyType( required MachII.framework.Event event ) output=false {
		
		var chooserId = event.getArg("ChooseID");
		var studyType = event.getArg("StudyType");
		var retaking = event.getArg("Retaking", "0");
		
		var sql = queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "	SELECT
						sc.choose_prop
					FROM
						t_student_course sc
					WHERE
						sc.scc_id = :chooserId ";
						
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "chooserId", value = chooserId, cfsqltype = "cf_sql_varchar");
			rs_chooser = queryObj.execute(sql = sql).getResult();
			
			
			/* 设置选课记录相关属性 */
			var chooserProp = getchoosePropertyAdvice().init();
			
			chooserProp.parseProperty( rs_chooser.choose_prop );
			
			/* 手工添加课表不允许学生修改 */
			chooserProp.setLocked( true );

			chooserProp.setPrePaid( false ); /* 是否从预交学费扣除 */
			chooserProp.setPaid( false ); /* 是否已收费 */
			
			/* 设置课程是否为预交学费代扣项目 N-正常选课 F-补修 E-提前毕业 */
			if ( studyType eq "N" or studyType eq "F" or studyType eq "E" ) {
				chooserProp.setPrePaid( true ); /* 是否从预交学费扣除 */
				chooserProp.setPaid( true ); /* 是否已收费 */
			}
			else {
				/* 其他类型所有选课 A-辅修 S-双学位 R-重修 都为需缴费项目 */
				chooserProp.setPrePaid( false ); /* 是否从预交学费扣除 */
				chooserProp.setPaid( false ); /* 是否已收费 */
			}
			
			sql = "	UPDATE
						t_student_course sc
					SET
						sc.st = :studyType ,
						sc.retaking = :retaking ,
						sc.choose_prop = :chooseProp 
					WHERE
						sc.scc_id = :chooserId ";
						
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "chooserId", value = chooserId, cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "chooseProp", value = chooserProp.stringProperty(), cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "studyType", value = studyType, cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "retaking", value = retaking, cfsqltype = "cf_sql_char");
			queryObj.execute(sql = sql);	

		}

	}

	/**
	* @hint "删除学生某学期选课记录 - 用于学籍异动 手工调整学生课表"
	* 
	* @event "MachII Event Object"
	*/
	public void function removeStudentCourseAll( required MachII.framework.Event event ) output=false {
		
		var studentId = event.getArg("StudentID");
		var termId = event.getArg("TermID");
		
		var sql = queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			/* 读取学生的学期选课记录 */
			
			sql = "SELECT a.task_type,a.task_id 
					FROM t_student_course a 
					WHERE a.term_id = :termId 
							AND 
							a.stu_id = :studentId ";
							
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "studentId", value = studentId, cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "termId", value = termId, cfsqltype = "cf_sql_char");
			var rs_chooser = queryObj.execute(sql = sql).getResult();
			
			for ( var a=1; a LTE rs_chooser.recordCount; a++ ) {
				
				/* 刷新教学班已选人数 */
			
				switch(rs_chooser["task_type"][a]){
				case "C":
					sql = "SELECT task_chooesd 
							FROM t_common_task 
							WHERE tsk_id = :taskId ";
					break;
	
				case "P":
					sql = "SELECT task_chooesd 
							FROM t_task 
							WHERE tsk_id = :taskId ";
					break;
				
				case "R":
					sql = "SELECT task_chooesd 
							FROM t_reopen_task 
							WHERE tsk_id = :taskId ";
					break;
				
				case "T":
					sql = "SELECT task_chooesd 
							FROM t_sport_task  
							WHERE tsk_id = :taskId ";
					break;
				}
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="taskId", value= rs_chooser["task_id"][a], cfsqltype="cf_sql_varchar" );
				rs_task = queryObj.execute( sql=sql ).getResult();
			
				switch(rs_chooser["task_type"][a]){
				case "C":
					sql = "UPDATE t_common_task a 
							SET a.task_chooesd = :RealSize  
							WHERE a.tsk_id = :taskId ";
					break;
	
				case "P":
					sql = "UPDATE t_task a 
							SET a.task_chooesd = :RealSize  
							WHERE a.tsk_id = :taskId ";
					break;
				
				case "R":
					sql = "UPDATE t_reopen_task a 
							SET a.task_chooesd = :RealSize  
							WHERE a.tsk_id = :taskId ";
					break;
				
				case "T":
					sql = "UPDATE t_sport_task a 
							SET a.task_chooesd = :RealSize  
							WHERE a.tsk_id = :taskId ";
					break;
				}
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="taskId", value=rs_chooser["task_id"][a], cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="RealSize", value=rs_task.task_chooesd - 1, cfsqltype="cf_sql_varchar" );
				queryObj.execute( sql=sql );
				
				sql = "DELETE 
						FROM t_student_course a
						WHERE a.term_id = :termId 
								AND 
								a.stu_id = :studentId  ";
					
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name = "studentId", value = studentId, cfsqltype = "cf_sql_varchar");
				queryObj.addParam(name = "termId", value = termId, cfsqltype = "cf_sql_char");
				queryObj.execute(sql = sql);	
				
			}
			
			
		}
		
	}
	
	
	/**
	* @hint "预制学生某学期必修课选课记录 - 用于学籍异动 手工调整学生课表"
	* 
	* @event "MachII Event Object"
	*/
	public void function presentStudentCourseAll( required MachII.framework.Event event ) output=false {
		
		var studentId = event.getArg("StudentID");
		var termId = event.getArg("TermID");
		
		var timeStamp = getdatetimeAdvice().getCurrentDatetimeString();
		
		var sequenceStudentCourse = getsequenceStudentCourse();
		var sequenceProgramTask = getsequenceProgramTask();
		
		var sql = queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			/* 学生基础信息 */
			
			sql = "SELECT a.cls_id,a.sbj_direction 
					FROM t_student a 
					WHERE a.stu_id = :studentId ";
					
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "studentId", value = studentId, cfsqltype = "cf_sql_varchar");
			
			var rs_student = queryObj.execute(sql = sql).getResult();
			
			/* 载入班级必修课课程计划 */
			
			sql = "SELECT 
					    a.cid, a.cidx, a.sbj_direction 
					FROM t_plan a  
					WHERE 
						a.prop_id = '1' 
						AND 
						a.cls_id = :classId 
						AND 
						a.term_id = :termId 
						AND 
						(a.cidx > 'XXX' OR a.cidx < 'XXX') 
						AND 
						(a.cidx > '000' OR a.cidx < '000') ";
						
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "classId", value = rs_student.cls_id, cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "termId", value = termId, cfsqltype = "cf_sql_char");
			
			var rs_plan = queryObj.execute(sql = sql).getResult();
			
			for ( var a=1; a LTE rs_plan.recordCount; a++ ) {
				
				var courseId = rs_plan["cid"][a];
				var courseIdx = rs_plan["cidx"][a];
					
				var planDir = rs_plan["sbj_direction"][a];

				sequenceProgramTask.setTermID(termId);
				sequenceProgramTask.setCourseID(courseId);
				sequenceProgramTask.setCourseIndex(courseIdx);
				
				var taskId = sequenceProgramTask.getID();
				
				var studentDir = rs_student.sbj_direction;
				
				/* 如果课程不分方向 或 方向与学生方向相同 */
				if ( listFind(planDir, studentDir, "/") or planDir eq "0") {
					
					sequenceStudentCourse.setStudentID(studentId);
					sequenceStudentCourse.setCourseID(courseId);
					sequenceStudentCourse.setTermID(termId);
					sequenceStudentCourse.setTaskType("P");
					
					var chooserId =sequenceStudentCourse.getID();
					
					sql = "SELECT a.choose_prop 
							FROM t_student_course a 
							WHERE a.scc_id = :chooserId ";
							
					queryObj = new Query(datasource = application.dnsMaster);
					queryObj.addParam(name = "chooserId", value = chooserId, cfsqltype = "cf_sql_varchar");
					
					var rs_studentCourse = queryObj.execute(sql = sql).getResult();
					
					if ( rs_studentCourse.recordCount EQ 0 ) {
						
						var prop = getchoosePropertyAdvice().init();

						/* 预置课表是否锁定 */
						prop.setLocked(true);
						/* 缴费状态设置 */
						prop.setPrePaid( true ); /* 是否从预交学费扣除 */
						prop.setPaid( true ); /* 是否已收费 */
						
						/* 新增选课记录 */
				
						sql = "INSERT INTO t_student_course (
									scc_id,
									task_type,
									cidx,
									task_id,
									st,
									retaking,
									choose_time,
									charge_time,
									choose_prop,
									prop_id,
									term_id,
									cid,
									ct,
									stu_id
								) VALUES (
									:ChooseID,
									:TaskType,
									:CourseIndex,
									:TaskID,
									:StudyType,
									:Retaking,
									:ChooseTime,
									:ChargeTime,
									:ChooseProp,
									:CourseProperty,
									:Term,
									:Course,
									:ChooseTurn,
									:Student
								)";
						queryObj = new Query(datasource = application.dnsMaster);
						queryObj.addParam(name = "ChooseID", value = chooserId, cfsqltype = "cf_sql_varchar");
						queryObj.addParam(name = "TaskType", value = "P", cfsqltype = "cf_sql_char");
						queryObj.addParam(name = "CourseIndex", value = courseIdx, cfsqltype = "cf_sql_char");
						queryObj.addParam(name = "TaskID", value = taskId, cfsqltype = "cf_sql_varchar");
						queryObj.addParam(name = "StudyType", value = "N", cfsqltype = "cf_sql_char");
						queryObj.addParam(name = "Retaking", value = "0", cfsqltype = "cf_sql_char");
						queryObj.addParam(name = "ChooseTime", value = timeStamp, cfsqltype = "cf_sql_char");
						queryObj.addParam(name = "ChargeTime", value = timeStamp, cfsqltype = "cf_sql_char");
						queryObj.addParam(name = "ChooseProp", value = prop.stringProperty(), cfsqltype = "cf_sql_varchar");
						queryObj.addParam(name = "CourseProperty", value = "1", cfsqltype = "cf_sql_char");
						queryObj.addParam(name = "Term", value = termId, cfsqltype = "cf_sql_char");
						queryObj.addParam(name = "Course", value = courseId, cfsqltype = "cf_sql_varchar");
						queryObj.addParam(name = "ChooseTurn", value = "9", cfsqltype = "cf_sql_char");
						queryObj.addParam(name = "Student", value = studentId, cfsqltype = "cf_sql_varchar");
						
						queryObj.execute(sql = sql);
					}
					else {
						
						/* 更新选课记录 */
						var prop = getchoosePropertyAdvice().init();
						prop.parseProperty( rs_studentCourse.choose_prop );
						
						/* 预置课表是否锁定 */
						prop.setLocked(true);
						/* 缴费状态设置 */
						prop.setPrePaid( true ); /* 是否从预交学费扣除 */
						prop.setPaid( true ); /* 是否已收费 */
						
						/*Unique: STU_ID, CID, TERM_ID, TASK_TYPE*/
					
						sql = "UPDATE t_student_course 
								SET choose_prop = :ChooseProp ,
									cidx = :CourseIndex ,
									task_id = :TaskID,
									st = :StudyType ,
									retaking = :Retaking,
									choose_time = :ChooseTime,
									charge_time = :ChargeTime,
									ct = :ChooseTurn,
									prop_id = :CourseProperty 
								 WHERE scc_id = :chooserId  ";
								 
						queryObj = new Query(datasource = application.dnsMaster);
						queryObj.addParam(name = "chooserId", value = chooserId, cfsqltype = "cf_sql_varchar");
						queryObj.addParam(name = "ChooseProp", value = prop.stringProperty(), cfsqltype = "cf_sql_varchar");
						queryObj.addParam(name = "CourseIndex", value = courseIdx, cfsqltype = "cf_sql_char");
						queryObj.addParam(name = "TaskID", value = taskId, cfsqltype = "cf_sql_varchar");
						queryObj.addParam(name = "StudyType", value = "N", cfsqltype = "cf_sql_char");
						queryObj.addParam(name = "Retaking", value = "0", cfsqltype = "cf_sql_char");
						queryObj.addParam(name = "ChooseTime", value = timeStamp, cfsqltype = "cf_sql_char");
						queryObj.addParam(name = "ChargeTime", value = timeStamp, cfsqltype = "cf_sql_char");
						queryObj.addParam(name = "ChooseTurn", value = "9", cfsqltype = "cf_sql_char");
						queryObj.addParam(name = "CourseProperty", value = "1", cfsqltype = "cf_sql_char");
						
						queryObj.execute(sql = sql);
						
					}
				
				}
				
				/* 完成一个课程预制后 刷新教学班数据记录 */
				
				sql = "SELECT 1
						FROM t_task a 
						WHERE a.tsk_id = :taskId";
							
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name = "taskId", value = taskId, cfsqltype = "cf_sql_varchar");
				
				var rs_task = queryObj.execute(sql = sql).getResult();
			
				if (rs_task.recordCount GT 0) {
						
					/* 每完成一批班级学生添加 刷新一次教学班已选人数 */
				
					sql = "SELECT count(*) stuC_count 
							FROM t_student_course a  
							WHERE a.task_id = :taskId ";
							
					queryObj = new Query(datasource = application.dnsMaster);
					queryObj.addParam(name = "taskId", value = taskId, cfsqltype = "cf_sql_varchar");
					
					var rs_taskCount = queryObj.execute(sql = sql).getResult();
						
					if ( rs_taskCount.recordCount ) {
						
						sql = "UPDATE t_task a 
								SET a.task_chooesd = :RealSize 
								WHERE a.tsk_id = :taskId ";
								
						queryObj = new Query(datasource = application.dnsMaster);
						queryObj.addParam(name = "taskId", value = taskId, cfsqltype = "cf_sql_varchar");
						queryObj.addParam(name = "RealSize", value = rs_taskCount["stuC_count"][1], cfsqltype = "cf_sql_varchar");
						
						queryObj.execute(sql = sql);
					}
				}
				
			}
			
				
		}
		
	}
	
	
	/**
	* @hint "删除学生选课记录 - 用于学籍异动 手工调整学生课表"
	* 
	* @event "MachII Event Object"
	*/
	public void function removeStudentCourse( required MachII.framework.Event event ) output=false {
		
		var studentId = event.getArg("StudentID");
		
		var taskId = event.getArg("TaskID");
		var taskType = event.getArg("TaskType");
		
		var chooserId = event.getArg("ChooseID");
		
		var sql = queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {

			sql = "DELETE 
					FROM t_student_course a
					WHERE a.scc_id = :chooserId 
					";
					
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "chooserId", value = chooserId, cfsqltype = "cf_sql_varchar");
			queryObj.execute(sql = sql);	

			/* 刷新教学班已选人数 */
			
			switch(taskType){
				case "C":
					sql = "SELECT task_chooesd 
							FROM t_common_task 
							WHERE tsk_id = :taskId ";
					break;
	
				case "P":
					sql = "SELECT task_chooesd 
							FROM t_task 
							WHERE tsk_id = :taskId ";
					break;
				
				case "R":
					sql = "SELECT task_chooesd 
							FROM t_reopen_task 
							WHERE tsk_id = :taskId ";
					break;
				
				case "T":
					sql = "SELECT task_chooesd 
							FROM t_sport_task  
							WHERE tsk_id = :taskId ";
					break;
			}
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
			rs_task = queryObj.execute( sql=sql ).getResult();
			
			switch(taskType){
				case "C":
					sql = "UPDATE t_common_task a 
							SET a.task_chooesd = :RealSize  
							WHERE a.tsk_id = :taskId ";
					break;
	
				case "P":
					sql = "UPDATE t_task a 
							SET a.task_chooesd = :RealSize  
							WHERE a.tsk_id = :taskId ";
					break;
				
				case "R":
					sql = "UPDATE t_reopen_task a 
							SET a.task_chooesd = :RealSize  
							WHERE a.tsk_id = :taskId ";
					break;
				
				case "T":
					sql = "UPDATE t_sport_task a 
							SET a.task_chooesd = :RealSize  
							WHERE a.tsk_id = :taskId ";
					break;
			}
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="RealSize", value=rs_task.task_chooesd - 1, cfsqltype="cf_sql_varchar" );
			queryObj.execute( sql=sql );

		}
		
	}
	
	
	/**
	* @hint "添加学生选课记录"
	* 
	* @event "MachII Event Object"
	*/
	public void function addStudentCourse( required MachII.framework.Event event ) output=false {
		
		var sequenceStudentCourse = getsequenceStudentCourse();
		
		var logFactory = getlogFactory();
		
		var studentId = event.getArg("StudentID");
		var taskId = event.getArg("TaskID");
		var taskType = event.getArg("TaskType");
		
		var studyType = event.getArg("StudyType");
		var courseProperty = event.getArg("CourseProperty");
		var retaking = event.getArg("Retaking", "0");
		
		var sql = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			switch(taskType){
				case "C":
					sql = "SELECT cid, term_id, cidx 
							FROM t_common_task 
							WHERE tsk_id = :taskId ";
					break;
	
				case "P":
					sql = "SELECT cid, term_id, cidx 
							FROM t_task 
							WHERE tsk_id = :taskId ";
					break;
				
				case "R":
					sql = "SELECT cid, term_id, cidx 
							FROM t_reopen_task 
							WHERE tsk_id = :taskId ";
					break;
				
				case "T":
					sql = "SELECT cid, term_id, cidx 
							FROM t_sport_task  
							WHERE tsk_id = :taskId ";
					break;
			}
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
			rs_task = queryObj.execute( sql=sql ).getResult();

			/*生成主键*/
			sequenceStudentCourse.setStudentID(studentId);
			sequenceStudentCourse.setCourseID(rs_task.cid);
			sequenceStudentCourse.setCourseIdx(rs_task.cidx);
			sequenceStudentCourse.setTermID(rs_task.term_id);
			sequenceStudentCourse.setTaskType(taskType);
			
			var chooserId = sequenceStudentCourse.getID();
			
			sql = "SELECT 1 FROM t_student_course WHERE scc_id = :chooserId ";
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="chooserId", value=chooserId, cfsqltype="cf_sql_varchar" );
			rs_chooser = queryObj.execute( sql=sql ).getResult();
		
			var chooserProp = getchoosePropertyAdvice().init();
			/* 手工添加课表不允许学生修改 */
			chooserProp.setLocked( true );
			
			chooserProp.setPrePaid( true ); /* 是否从预交学费扣除 */
			chooserProp.setPaid( true ); /* 是否已收费 */
			
			/* 设置课程是否为预交学费代扣项目 N-正常选课 F-补修 E-提前毕业 */
			if ( studyType eq "N" or studyType eq "F" or studyType eq "E" ) {
				chooserProp.setPrePaid( true ); /* 是否从预交学费扣除 */
				chooserProp.setPaid( true ); /* 是否已收费 */
			}
			
			if ( rs_chooser.recordCount EQ 0 ) {

				sql = "INSERT INTO 
						   t_student_course(
						   		scc_id, 
								stu_id, 
								cid, 
								term_id, 
								prop_id, 
								task_type, 
								cidx, 
								task_id, 
								st,
								retaking, 
								ct,
								choose_time, 
								choose_prop, 
								charge_time )
						   VALUES(
						   		:chooserId, 
								:studentId, 
								:courseId, 
								:termId, 
								:propId,
								:taskType, 
								:courseIndex, 
								:taskId, 
								:studyType, 
								:retaking, 
								:chooseTurn, 
								:chooseTime, 
								:chooseProp, 
								:chargeTime 
						   )";
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="chooserId", value=chooserId, cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="studentId", value=studentId, cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="courseId", value=rs_task.cid, cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="termId", value=rs_task.term_id, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="propId", value=courseProperty, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="taskType", value=taskType, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="courseIndex", value=rs_task.cidx, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="studyType", value=studyType, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="retaking", value=retaking, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="chooseTurn", value="9", cfsqltype="cf_sql_char" );
				queryObj.addParam( name="chooseTime", value=getdatetimeAdvice().getCurrentDatetimeString(), cfsqltype="cf_sql_char" );
				queryObj.addParam( name="chooseProp", value=chooserProp.stringProperty(), cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="chargeTime", value=getdatetimeAdvice().getCurrentDatetimeString(), cfsqltype="cf_sql_char" );
				
				queryObj.execute( sql=sql );
				
				sql = "SELECT course_name FROM t_course WHERE cid = :courseId  ";
				
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="courseId", value=rs_task.cid, cfsqltype="cf_sql_varchar" );
				
				rs_course = queryObj.execute( sql=sql ).getResult();
				
				content = "给学生选取课程 " & rs_course.course_name;
				
				logFactory.stuCourse('8',content,studentId,rs_task.cid,rs_task.term_id);
						   
			}
			else {
				sql = "UPDATE 
						   t_student_course
						SET
							prop_id = :propId, 
							task_type = :taskType, 
							cidx = :courseIndex, 
							task_id = :taskId, 
							st = :studyType,
							retaking = :retaking, 
							ct = :chooseTurn,
							choose_time = :chooseTime, 
							choose_prop = :chooseProp, 
							charge_time = :chargeTime 
						WHERE 
							scc_id = :chooserId ";
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="chooserId", value=chooserId, cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="propId", value=courseProperty, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="taskType", value=taskType, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="courseIndex", value=rs_task.cidx, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="studyType", value=studyType, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="retaking", value=retaking, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="chooseTurn", value="9", cfsqltype="cf_sql_char" );
				queryObj.addParam( name="chooseTime", value=getdatetimeAdvice().getCurrentDatetimeString(), cfsqltype="cf_sql_char" );
				queryObj.addParam( name="chooseProp", value=chooserProp.stringProperty(), cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="chargeTime", value=getdatetimeAdvice().getCurrentDatetimeString(), cfsqltype="cf_sql_char" );
				
				queryObj.execute( sql=sql );

			}
			
			/* 刷新课程教学班已选人数 */
			sql = "	SELECT
					   COUNT(*) r_count
				   FROM
					   t_student_course sc
				   WHERE
					   sc.task_id = :taskId ";
		   
		   queryObj = new Query( datasource=application.dnsMaster );
		   
		   queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
		   
		   rs_taskCountEntity = queryObj.execute( sql = sql ).getResult();

		   if( rs_taskCountEntity.recordCount ){
			   sql = "	UPDATE
						   t_task t
					   SET 
						   t.task_chooesd = :realSize 
					   WHERE
						   t.tsk_id = :taskId ";
						   
			   queryObj = new Query( datasource=application.dnsMaster );
			   
			   queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
			   queryObj.addParam( name="realSize", value=rs_taskCountEntity.r_count, cfsqltype="cf_sql_integer" );
			   
			   queryObj.execute( sql = sql );
		   }
			
			
		}
		
	}
	
	
	/**
	* @hint "删除学生选课记录"
	* 
	* @event "MachII Event Object"
	*/	
	public void function taskRemoveStudent( required MachII.framework.Event event ) output=false {
		
		var taskId = event.getArg("TSK");
		var studentId = event.getArg("STU");
		var chooserId =  event.getArg("chooser");

		var passArg = structNew();
		var sql = "";
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "SELECT a.term_id, a.cid, b.course_name FROM t_task a INNER JOIN t_course b ON b.cid = a.cid WHERE tsk_id = :taskId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			
			queryObj.addParam(name = "taskId", value = taskId, cfsqltype = "cf_sql_varchar");
			
			rs_task = queryObj.execute(sql = sql).getResult();
			
			
			/* 删除选课记录 */
			sql = "	DELETE FROM
						t_student_course sc
					WHERE
						sc.scc_id = :chooserId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			
			queryObj.addParam(name = "chooserId", value = chooserId, cfsqltype = "cf_sql_varchar");
			
			queryObj.execute(sql = sql);
			
			/* 删除成绩临时记录 */
			sql = "	DELETE FROM
						t_temp_mark t
					WHERE
						t.scc_id = :chooserId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "chooserId", value = chooserId, cfsqltype = "cf_sql_varchar");
			queryObj.execute(sql = sql);
			
			
			/* 删除对应成绩记录 */
			sql = "	DELETE FROM
						t_student_mark t
					WHERE
						t.scc_id = :chooserId ";

			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "chooserId", value = chooserId, cfsqltype = "cf_sql_varchar");
			queryObj.execute(sql = sql);
			
			sql = "	UPDATE
						  t_task t
					  SET
						  t.task_chooesd = t.task_chooesd - 1
					  WHERE
						  t.tsk_id = :taskId ";
						  
			queryObj = new Query(datasource = application.dnsMaster);
			
			queryObj.addParam(name = "taskId", value = taskId, cfsqltype = "cf_sql_varchar");
			
			queryObj.execute(sql = sql);
			
			content = "给学生删除课程 " & rs_task.course_name;
				
			logFactory.stuCourse('8',content,studentId,rs_task.cid,rs_task.term_id);
				
			structInsert(passArg, "TSK", taskId);
			structInsert(passArg, "TT", "P");
			
	
			event.setArgs(passArg);
			
		}
	}

}

</cfscript>